package com.example.demo.controller;

import com.example.demo.yb.model.CustomerPanel;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.*;

/**
 * @author linjingze
 * @date 2024/11/21 11:05
 */
@Controller
public class DataController {

    public static Map<String, Map<Integer, BigDecimal>> map = new HashMap<>();

    public static Set<String> nameList = new HashSet<>();

    @GetMapping("/api/data")
    @ResponseBody
    public CustomerPanel customerViewList(String customer) {
        CustomerPanel p = new CustomerPanel();
        List<String> list = new ArrayList<>(nameList);
        if (StringUtils.isNotBlank(customer)) {
            list = Arrays.stream(customer.split(",")).toList();
        }
        p.setNameList(list);
        Map<String, List<BigDecimal>> customerVolMap = new HashMap<>();

        for (Map.Entry<String, Map<Integer, BigDecimal>> e : map.entrySet()) {
            String key = e.getKey();
            Map<Integer, BigDecimal> valueMap = e.getValue();
            List<BigDecimal> vList = new ArrayList<>();
            for (int i = 1; i <= 12; i++) {
                BigDecimal bigDecimal = valueMap.getOrDefault(i, BigDecimal.ZERO);
                vList.add(bigDecimal);
            }
            customerVolMap.put(key, vList);
        }
        p.setCustomerVolMap(customerVolMap);
        return p;
    }

    @GetMapping("/customer/page")
    public String customerPage(String customer, Model model) {
        model.addAttribute("customer", customer);
        return "customerView";
    }

}
